package com.application.Activity;

import java.util.ArrayList;

import ReadWriteXML.CMember;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.Custom.ListUserAdapter;
import com.application.Network.Protocol;
import com.application.PhanQuyen.Quyen;
import com.application.outdoor.InvoikeServices.LogThaoTacInvoike;
import com.application.outdoor.pojo.LogUtil;

public class frmDanhSachThanhVienActivity extends Activity {
	int port = Protocol.PORT;
	ListView lvDanhSachThanhVien;

	ArrayList<CMember> memberList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_danh_sach_thanh_vien);
		lvDanhSachThanhVien = (ListView) findViewById(R.id.lvDanhSachThanhVien);
		fillListView();
		registerForContextMenu(lvDanhSachThanhVien);
	}

	@Override
	public void onStart() {
		super.onStart();
		ActivityController.setPeerActivity(this);
		ActivityController.addActivityToArray(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		ActivityController.setPeerActivity(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		ActivityController.setPeerActivity(this);
	}

	private void fillListView() {
		memberList = ReadXML.ReadMember(ReadWriteFileController.MEMBER_PATH);
//		ArrayAdapter<CMember> adapter = new ArrayAdapter<CMember>(this, android.R.layout.simple_list_item_1, memberList);
		ListUserAdapter adapter = new ListUserAdapter(this, R.layout.list_rolelist, memberList);
		lvDanhSachThanhVien.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

		if (v.getId() == R.id.lvDanhSachThanhVien) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			if (NetworkController._peer.getAccount().get_Username()
					.equals(memberList.get(info.position).get_userName())) {
					Toast.makeText(getApplicationContext(), "Bạn không thể thao tác với chính mình.", Toast.LENGTH_LONG).show();
			} else {
				menu.setHeaderTitle("Thao tác");
				menu.addSubMenu(Menu.NONE, 1, 1, "Mời ra khỏi nhóm");
				menu.addSubMenu(Menu.NONE, 2, 2, "Chat riêng");
				menu.addSubMenu(Menu.NONE, 3, 3, "Yêu cầu ngừng thảo luận");
				menu.addSubMenu(Menu.NONE, 4, 4, "Đặt vai trò");
			}
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		if (menuItemIndex == 1) {
			Log.d("Phu", "vao moi nguoi khac ra ngoai");
			Quyen myRule = NetworkController._peer.getAccount().getRuleById(5);
			if (myRule != null && myRule.getSend()) {
				Log.d("Phu", "co quyen moi nguoi khac ra ngoai");
			//	Toast.makeText(getBaseContext(), "mời ra khỏi nhóm", 1).show();
				Intent data = new Intent();
				data.putExtra(frmTreeNodeActivity.IP_RESULT, memberList.get(info.position).get_ip());
				data.putExtra(frmTreeNodeActivity.USERNAME_RESULT, memberList.get(info.position).get_userName());
				setResult(frmTreeNodeActivity.CODE_KICK, data);
				// startActivity(data);
				
				finish();
				//Ghi log
				if(NetworkController.isInDoor()){
					ReadWriteFileController.WriteToLogFile("Mời "+memberList.get(info.position).get_userName()+"ra khỏi nhóm");
				}else if(NetworkController.isOutDoor()){					
					new LogThaoTacInvoike().insert(CommonUtilities.nguoiDung.getTenDangNhap(),memberList.get(info.position).get_userName(), CommonUtilities.idCayHienTai, LogUtil.MOI_RA_KHOI_NHOM);
				}
			} else {
			//	finish();
				Show(Protocol.NON_RULE_MESSAGE);
				//Ghi log
				if(NetworkController.isInDoor()){
					ReadWriteFileController.WriteToLogFile("Không có quyền mời người khác ra khỏi nhóm!");
				}else if(NetworkController.isOutDoor()){					
					new LogThaoTacInvoike().insert(CommonUtilities.nguoiDung.getTenDangNhap(),memberList.get(info.position).get_userName()+":"+LogUtil.NONE_RULE, CommonUtilities.idCayHienTai, LogUtil.MOI_RA_KHOI_NHOM);
				}
			}
		} else if (menuItemIndex == 2) {
			Log.d("Phu", "vao chat rieng");
			Quyen myRule = NetworkController._peer.getAccount().getRuleById(7);
			if (myRule != null && myRule.getSend()) {
				// frmChatPeerActivity._IPFriend=memberList.get(info.position).get_ip();
				// frmChatPeerActivity._userNameFriend=memberList.get(info.position).get_userName();
				Intent data = new Intent(this, frmChatPeerActivity.class);
				data.putExtra(frmChatPeerActivity.IP_CHAT_PEER, memberList.get(info.position).get_ip());
				data.putExtra(frmChatPeerActivity.USERNAME, memberList.get(info.position).get_userName());
				setResult(frmChatPeerActivity.CODE_CHATPEER, data);

				startActivity(data);
				finish();				
				
			} else {
				finish();
				NetworkController.ShowMessageNoticeTAB(Protocol.NON_RULE_MESSAGE);
			}
		} else if (menuItemIndex == 3) {
			Log.d("Phu", "vao yeu cau ngung thao luan");
			Quyen quyen = NetworkController._peer.getAccount().getRuleById(6);
			if (quyen != null && quyen.getSend()) {
				Log.d("Phu", "co quyen yeu cau ngung thao luan");
				//Toast.makeText(getBaseContext(), "Yêu cầu ngừng thảo luận", 1).show();
				Intent data = new Intent();
				data.putExtra(frmTreeNodeActivity.IP_RESULT, memberList.get(info.position).get_ip());
				data.putExtra(frmTreeNodeActivity.USERNAME_RESULT, memberList.get(info.position).get_userName());
				setResult(frmTreeNodeActivity.CODE_STOP_TALK, data);
				// startActivity(data);
				// startActivityForResult(data, FrmTabActivity.CODE_STOP_TALK);
				finish();
				//Ghi log
				if(NetworkController.isInDoor()){
					ReadWriteFileController.WriteToLogFile("Yêu cầu "+memberList.get(info.position).get_userName()+"ngừng thảo luận");
				}else if(NetworkController.isOutDoor()){					
					new LogThaoTacInvoike().insert(CommonUtilities.nguoiDung.getTenDangNhap(),memberList.get(info.position).get_userName(), CommonUtilities.idCayHienTai, LogUtil.NGUNG_THAO_LUAN);
				}
			} else {
			//	finish();
				Show(Protocol.NON_RULE_MESSAGE);
				//Ghi log
				if(NetworkController.isInDoor()){
					ReadWriteFileController.WriteToLogFile("Không có quyền Yêu cầu ngừng thảo luận");
				}else if(NetworkController.isOutDoor()){					
					new LogThaoTacInvoike().insert(CommonUtilities.nguoiDung.getTenDangNhap(),memberList.get(info.position).get_userName()+":"+LogUtil.NONE_RULE, CommonUtilities.idCayHienTai, LogUtil.NGUNG_THAO_LUAN);
				}
			}

		}else if(menuItemIndex==4){		
			
				Quyen setRoleChoNguoiKhac = NetworkController._peer.getAccount().getRule(Protocol.R_SET_VAI_TRO);
				if (NetworkController.getIsThanhLapNhom() || (setRoleChoNguoiKhac != null && setRoleChoNguoiKhac.getSend())) {
					Intent intent = new Intent(this, frmSetRoleActivity.class);
					intent.putExtra(frmNhomChoActivity.NAME_CODE, memberList.get(info.position).get_fullName());
					intent.putExtra(frmNhomChoActivity.IP_CODE, memberList.get(info.position).get_ip());
					intent.putExtra(Protocol.NGUOI_DUNG, memberList.get(info.position).get_userName());
					startActivity(intent);
				} else {
					Show(Protocol.NON_RULE_MESSAGE);
				}
			
		}
		return true;
	}

	public void Show(String message) {
		// Toast.makeText(this, "hehe Xin chao", Toast.LENGTH_SHORT).show();
		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		dlg.show();
	}
}
